﻿using EF6Query.Models.Common;
using EF6Query.Models.Req;
using EF6Query.QueryService;
using ExpressionBuilder.Helpers;
using Microsoft.AspNetCore.Mvc;
using WebEasyUI.Models.DBModel;

namespace WebEasyUI.Controllers
{
    public class ExpressionApplicationController : Controller
    {
        /// <summary>
        /// 模拟数据库查询出用户数据信息
        /// </summary>
        /// <returns></returns>
        [HttpPost, HttpGet]
        public List<UserInfo> GetUserInfoDataDB()
        {
            List<UserInfo> users = new();
            for (int i = 0; i < 100; i++)
            {
                UserInfo user = new()
                {
                    Id = Guid.NewGuid(),
                    Gender = i % 2,
                    UserName = "张三" + i,
                    CteateTime = DateTime.Now.AddDays(-i),
                };
            }
            return users;
        }


        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <returns></returns>
        [HttpPost, HttpGet]
        public IActionResult GetProductView(ExpressionBuilderRequest request)
        {
            var result = new ProductService().ExpressionBuilderProductList(request);
            return Json(result);
        }



        /// <summary>
        /// 过滤查询获取产品信息
        /// </summary>
        /// <returns></returns>
        [HttpPost, HttpGet]
        public IActionResult ProductFifter(List<DynamicExpressionRequest> request)
        {
            var result = new ProductService().ProductFifter(request);
            return Json(result);
        }


        /// <summary>
        /// 过滤查询获取产品信息
        /// </summary>
        /// <returns></returns>
        [HttpPost, HttpGet]
        public IActionResult ProductByDynamicExpression(DynamicRequest request)
        {
            var result = new ProductService().ProductFifter(request.Data);
            return Json(result);
        }

        /// <summary>
        /// 获取表数据
        /// </summary>
        /// <returns></returns>
        [HttpPost, HttpGet]
        public IActionResult GetTables()
        {
            var obj = new[]
            {
               new{ Id = 1,  text ="Product" },
               new{ Id = 2,  text ="ProductColor" },
            };
            return Ok(obj);
        }

        /// <summary>
        /// 根据表名获取属性信息
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        [HttpPost, HttpGet]
        public IActionResult GetTablePropertystring(string tableName)
        {
            var reslut = new DBEntityType().GetTypePropertyByTableName(tableName);
            return Json(reslut);
        }


        /// <summary>
        /// 根据表名获取属性信息
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="propertyName">属性名称</param>
        /// <returns></returns>
        [HttpPost, HttpGet]
        public IActionResult GetTablePropertyInfos(string tableName, string propertyName)
        {
            var propertyInfo = new DBEntityType().GetTypePropertyInfo(tableName);
            var propertyFirst = propertyInfo.FirstOrDefault(x => x.PropertyName == propertyName);
            if (propertyFirst != null)
            {
                var supportedOperations = new OperationHelper()
                                .SupportedOperations(propertyFirst.PropertyType)
                                .Select(o => new
                                {
                                    Id = o.ToString(),
                                    Name = o.Name
                                })
                                .ToList();
                return Json(supportedOperations);
            }
            return Json(null);
        }
    }
}
